<?php
namespace app\api\controller\login;

use app\api\controller\Member;
use app\common\model\CourseAttendanceModel;
use app\common\model\CourseModel;
use app\common\model\SchoolClassModel;
use app\common\model\SchoolGradeModel;
use app\common\model\UserModel;
use think\db;

class Classmaster extends Member
{

    /**
     * 班主任的话计算学生数与课程数
     */
    public function getTimeCount(){
        $user = $this->getMember();

        $student_count = 0;
        $course_count = 0;
        $attendance_count = 0;
        $attendance = [];
        if($user['grade_id'] > 0 && $user['class_id'] > 0){
            $student_count = UserModel::where('school_id',$user['school_id'])
                ->where('grade_id',$user['grade_id'])
                ->where('class_id',$user['class_id'])
                ->where('student_no is not null and student_no !=""')
                ->where('role',1)->count();

            $course_count =  CourseModel::where('school_id',$user['school_id'])
                ->where('grade_id',$user['grade_id'])
                ->where('class_id',$user['class_id'])
                ->count();

            //缺勤率
            $course_ids = CourseModel::where('school_id',$user['school_id'])
                ->field('group_concat(id) as course_ids')->find()['course_ids'];
            //$attendance_count = CourseAttendanceModel::where('course_id','in',$course_ids)->count();
            $attendance = CourseAttendanceModel::where('course_id','in',$course_ids)->select();

        }
        $result['student_count'] = $student_count;
        $result['course_count'] = $course_count;

        $totoal_rate = 0;
        $in_rate = 0;
        $away_rate = 0;
        foreach ($attendance as $key=>$value){
            if(count(explode(',',$value['all_userids']))!=0){
                $away_rate += UserModel::where('id', 'in', explode(',',$value['away_userids']))
                    ->where('school_id',$user['school_id'])
                    ->where('grade_id',$user['grade_id'])
                    ->where('class_id',$user['class_id'])
                    ->count();
                $totoal_rate += UserModel::where('id', 'in', explode(',',$value['all_userids']))
                    ->where('school_id',$user['school_id'])
                    ->where('grade_id',$user['grade_id'])
                    ->where('class_id',$user['class_id'])
                    ->count();

                //$rate += count(explode(',',$value['away_userids']))/count(explode(',',$value['all_userids']));
            }
        }
        if($totoal_rate!=0){
            $attendance_rate = $away_rate/$totoal_rate;
        }else{
            $attendance_rate = 0;
        }
        $result['attendance_rate'] = round($attendance_rate,4)*100;
        $this->success('获取班主任的学生数与课程数与缺勤率成功！',$result,200);
    }

    /**
     * 班主任的话计算缺勤详细情况
     */
    public function getAttendance(){
        $user = $this->getMember();
        //得到班主任所管的班级的课程
        $course_ids = CourseModel::where('school_id',$user['school_id'])
            //->where('grade_id',$user['grade_id'])
            //->where('class_id',$user['class_id'])
            ->field('group_concat(id) as course_ids')->find()['course_ids'];
        //得到考勤列表
        $attendance = CourseAttendanceModel::where('course_id','in',$course_ids)
            ->order('create_time desc')->select();
        //循环拼成数组
//        $all_usernames = '';
//        $away_usernames = '';
        $result = [];
        foreach ($attendance as $key=>$value){
            $course = CourseModel::where('id',$value['course_id'])->find();
            $temp = [];
            $temp['key'] = $key+1;
            $temp['course_name'] = $course['name'];
            $temp['address'] = $course['address'];
            $temp['day'] = $value['day'];

            $away_users = UserModel::where('id', 'in', explode(',',$value['away_userids']))->select();
            foreach ($away_users as $away_user) {
                $temp['realname'] = $away_user['realname'];
                $result[] = $temp;
            }
        }
        $this->success('获取班主任所在班级的缺勤详细情况成功！',$result,200);
    }

    /**
     * 获取班主任的学生
     */
    public function getStudent()
    {
        $user = $this->getMember();
        $student = [];
        if($user['grade_id'] > 0 && $user['class_id'] > 0){
//            $student = UserModel::where('school_id',$user['school_id'])
//                ->where('grade_id',$user['grade_id'])
//                ->where('class_id',$user['class_id'])
//                ->where('role',1)->select();

            $student = UserModel::with(['schoolgrade', 'schoolclass'])
                ->join('edu_user_times t','t.user_id=edu_user.id','LEFT')
                ->join('edu_course c','t.course_id=c.id','LEFT')
                ->join('edu_school_class clas','clas.id=edu_user.class_id','LEFT')
                ->join('edu_user u2','u2.id=edu_user.pid','LEFT')
                ->where('edu_user.school_id', $user['school_id'])
                ->where('edu_user.grade_id', $user['grade_id'])
                ->where('edu_user.class_id', $user['class_id'])
                ->where('edu_user.role', 1)
                ->field('edu_user.*,u2.realname AS bind_name, GROUP_CONCAT(DISTINCT c.name) as course_name')
                ->group('edu_user.id')
                ->all();

            // 过滤学号为空
            $result = [];
            foreach ($student as $key => $item)
            {
                if(!empty($item['student_no'])){
                    $result[] = $item;
                }
            }
            $student = $result;
        }

        $this->success('获取班主任的学生成功！',$student,200);
    }

}